package team.cdlzz.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.cdlzz.model.pojo.User;
import team.cdlzz.service.UserService;

import java.util.List;

/**
 * @author zw
 * @date 2022-06-25.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public User Login(@RequestParam("mail")String mail , @RequestParam("pass")String pass){
        User login = userService.login(mail, pass);
        return login;
    }

    @RequestMapping("/login_u")
    public User Login(@RequestParam("uid")Integer uid , @RequestParam("pass")String pass){
        User login = userService.login_u(uid, pass);
        return login;
    }
    @RequestMapping("/getUser")
    public User getUser(@RequestParam("uid")Integer uid){
        User login = userService.getUser(uid);
        return login;
    }

    @RequestMapping("/testpage")
    public List<User> getPageInfo(@RequestParam(value = "pagenum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize ){
        return userService.findPage(pageNum,pageSize);
    }

    @RequestMapping("/userRegister")
    public String UserRegister(@RequestParam("mail") String mail, @RequestParam("pass") String pass, @RequestParam("verify") String verify) {
        return userService.UserRegister(mail, pass, verify);
    }

    @RequestMapping("/userInfo")
    public JSONObject UserInfo(@RequestParam("uid")Integer uid){
        return userService.getUserInfo(uid);
    }

    @RequestMapping("/userUpdata")
    public String UserUpdata(@RequestParam("Password")String password  ,
                                 @RequestParam("School")String school  ,@RequestParam("uid")Integer uid,
                             @RequestParam("Name")String name){

        if (password!=null&&  !"".equals(password)  &&
                school!=null&&!"".equals(school) && uid != null
                && uid != 0 && !"".equals(name)&& name!=null){
            User user = new User();
            user.setPwd(password);
            user.setName(name);
            user.setOrganization(school);
            user.setUid(uid);
            return userService.userUpdata(user);
        }
        return null;
    }


//  @RequestMapping("/userUpdata")
//    public String UserUpdata(@RequestBody String url){
//      System.out.println(url);
//        return null;
//    }

}